package cn.kasa.system.controller;

import cn.kasa.basic.utils.AjaxResult;
import cn.kasa.basic.utils.PageList;
import cn.kasa.org.domain.Employee;
import cn.kasa.system.domain.Dictionary;
import cn.kasa.system.query.DictionaryQuery;
import cn.kasa.system.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private IDictionaryService dictionaryService;

    @GetMapping
    public List<Dictionary> loadAll(){
       return dictionaryService.loadAll();
    }

    @GetMapping("/{id}")
    public Dictionary loadById(@PathVariable("id") Long id){
        return dictionaryService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionaryService.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    @PatchMapping
    public AjaxResult batchDictionary(@RequestBody  Long[] ids){
        try {
            dictionaryService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping
    public PageList<Dictionary> queryData(@RequestBody DictionaryQuery dictionaryQuery){
        PageList<Dictionary> dictionaryPageList = dictionaryService.queryData(dictionaryQuery);
        return dictionaryPageList;
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Dictionary dictionary){
        try{
            if(dictionary.getId() == null){
                dictionaryService.save(dictionary);
            }else {
                dictionaryService.update(dictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult remove2(@PathVariable("id") Long id){
        try {
            dictionaryService.remove2(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

}
